package indi.zhifa.study2025.test.rabbitmq.dm.consumer;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.*;
import indi.zhifa.study2025.test.rabbitmq.common.Constant;
import indi.zhifa.study2025.test.rabbitmq.common.entity.OrderMsg;
import indi.zhifa.study2025.test.rabbitmq.common.utils.NWRabbitUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.time.LocalDateTime;

@Slf4j
public class DMConsumerApplication {

    public static void main(String[] args) throws IOException {
        Connection connection = null;
        try{
            connection = NWRabbitUtils.getConnection();
            if(null == connection){
                return;
            }
            Channel channel = connection.createChannel();
            // 消息预加载
            channel.basicQos(1);
            channel.basicConsume(Constant.DM_QUE_NAME,false,new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String str = new String(body);
                    OrderMsg orderMsg = JSON.parseObject(str,OrderMsg.class);
                    log.info("现在时间是"+ LocalDateTime.now() +"接收到订单超时信息："+orderMsg);
                    channel.basicAck(envelope.getDeliveryTag(),false);
                }
            });
        }catch (Exception e){
            System.out.println(e);
        }
    }

}
